package logic

import (
	"context"
	"encoding/json"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"month/models"

	"month/rpc/internal/svc"
	"month/rpc/month"

	"github.com/zeromicro/go-zero/core/logx"
)

type HistoryFindLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewHistoryFindLogic(ctx context.Context, svcCtx *svc.ServiceContext) *HistoryFindLogic {
	return &HistoryFindLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 历史查询
func (l *HistoryFindLogic) HistoryFind(in *month.HistoryFindRequest) (*month.EsResponse, error) {
	// todo: add your logic here and delete this line

	commitId := in.CommitId
	commitText := in.CommitText

	var gh []models.FgitHis

	db, err := gorm.Open(mysql.Open("root:q025l6263@tcp(43.143.123.142:3306)/2107a"), &gorm.Config{})
	if err != nil {
		return &month.EsResponse{
			Code:    400,
			Message: "数据库连接失败",
		}, nil
	}

	//支持commit id和提交描述进行多条件搜索
	err = db.Raw("select * from `fgit_his` where `commit` = ? or `id` = ?", commitText, commitId).Scan(&gh).Error
	if err != nil {
		return &month.EsResponse{
			Code:    400,
			Message: "数据库查询失败",
		}, err
	}

	data, _ := json.Marshal(gh)
	return &month.EsResponse{
		Code:    200,
		Message: "成功",
		Data:    string(data),
	}, nil
}
